<?php

function ParseOnePerson($sBuffer, $db)
{
	try {
		$xml = new SimpleXMLElement($sBuffer);
	} catch (Exception $e) {
		return false;
	}

	if ($xml->{'is-organization'} == 'true')
		return true;

	$wellness = $xml->wellness;
	if ($wellness == 0)
		return true;
	
	$id =  $xml->id;
	$name = $xml->name;
	$citizen = $xml->citizenship->country->id;
	$residence = $xml->residence->country->id;
	$birth = $xml->{'date-of-birth'};
	
	$experience = $xml->{'experience-points'};
	
	$level = $xml->level;

	$workskill = $xml->{'work-skill-points'};
	if ($workskill == '') {
		$workskill = 0;
	}
	
	$rank = $xml->military->{'rank-points'};
	if ($rank == '') {
		$rank = 0;
	}
	
	$strength = '';
	foreach($xml->{'military-skills'}->{'military-skill'} as $skill) {
		switch ($skill->name) {
	    case 'Strength':
	        $strength = intval($skill->points);
					if ($strength == '') {
						$strength = 0;
					}
	        break;
		}
	}

	$hard_worker = 0;
	$president = 0;
	$congressman = 0;
	$battle_hero = 0;
	$media_mogul = 0;
	$society_builder = 0;
	$resistance_hero = 0;
	$super_trooper = 0;
	$campaign_hero = 0;
	if (isset($xml->medals->medal)) {
		foreach($xml->medals->medal as $medal) {
			$medal_type = $medal->type;
			switch ($medal_type) {
		    case 'hard worker':
		        $hard_worker = $medal->amount;
		        break;
		    case 'president':
		        $president = $medal->amount;
		        break;
		    case 'congressman':
		        $congressman = $medal->amount;
		        break;
		    case 'hero':
		        $battle_hero = $medal->amount;
		        break;
		    case 'media mogul':
		        $media_mogul = $medal->amount;
		        break;
		    case 'society builder':
		        $society_builder = $medal->amount;
		        break;
		    case 'resistance hero':
		        $resistance_hero = $medal->amount;
		        break;
		    case 'super trooper':
		        $super_trooper = $medal->amount;
		        break;
		    case 'campaign hero':
		        $campaign_hero = $medal->amount;
		        break;

			}
		}
	}
	
	$query = "INSERT INTO users(id,name,citizen,residence,birth,experience,"
	. "level,wellness,workskill,rank,strength,"
	. "m_worker,m_soldier,m_congress,m_president,m_media,m_builder,m_rhero,m_bhero,m_chero"
	. ") VALUES($id,'"
	. $db->escapeString($name) . "',"
	. "$citizen,$residence,'$birth',$experience,"
	. "$level,$wellness,$workskill,$rank,$strength,"
	. "$hard_worker,$super_trooper,$congressman,$president,$media_mogul,$society_builder,$resistance_hero,$battle_hero,$campaign_hero"
	. ")";
	
	$result = $db->exec($query);
	
	if (!$result) {
		echo "$query\n" . $db->lastErrorMsg() . "\n";
		return false;
	}
	
	return true;
}


function ParseOneCountry($sCountry, $db)
{
	$sFileXml = $sCountry . '_citizens.xml';
	$rFile = fopen($sFileXml, 'r');
	
	$BatchOk = 0;
	$BatchError = 0;
	$sBuffer = '';
	
	$db->exec('BEGIN');
	
	while($sLine = fgets($rFile)) {
		$sLine = trim($sLine);
		if ($sLine == '<citizen>') {
			$sBuffer = $sLine;
			while($sLine = fgets($rFile)) {
				$sLine = trim($sLine);
				$sBuffer .= $sLine;
				if ($sLine == '</citizen>') {
					break;
				}
			}
			$rv = ParseOnePerson($sBuffer,$db);
			if ($rv) {
				$BatchOk++;
			}
			else {
				$BatchError++;
			}
		}
	}
	
	$db->exec('COMMIT');
	
	echo "$sCountry parsed\n";
	echo "parsed=$BatchOk\n";
	echo "errors=$BatchError\n\n";
}


// main 

$db = new SQLite3('citizens.db');

$query = 'CREATE TABLE IF NOT EXISTS users (
id MEDIUMINT UNIQUE NOT NULL,
name VARCHAR(255) COLLATE NOCASE NOT NULL,
citizen TINYINT NOT NULL,
residence TINYINT,
birth TEXT,
experience MEDIUMINT,
level TINYINT,
wellness FLOAT,
workskill MEDIUMINT DEFAULT 0,
rank MEDIUMINT DEFAULT 0,
strength MEDIUMINT DEFAULT 0,
m_worker TINYINT,
m_soldier TINYINT,
m_congress TINYINT,
m_president TINYINT,
m_media TINYINT,
m_builder TINYINT,
m_rhero TINYINT,
m_bhero TINYINT,
m_chero TINYINT
)';

$db->exec($query);

$query = 'CREATE INDEX IF NOT EXISTS name ON users(name)';

$db->exec($query);

$countries = file('list.txt', FILE_IGNORE_NEW_LINES);

foreach($countries as $country) {
	ParseOneCountry($country, $db);
}

$db->close();

?>
